<template>
    <!--pages/todes/todos.wxml-->
    <view class="page">
        <view class="search">
            <view class="todes-box">
                <label for="search" class="todes-sea" @tap="searchFun">
                    <span class="iconfont icon-sousuo1"></span>
                </label>
                <input id="search" placeholder="搜索新闻" class="todes-inp" @confirm="searchFun" @focus="hiddenFun" @blur="hiddenFun" :value="search" />
                <!--<view class="todes {{hidden?'todes-hidden':''}}">
            <span class="iconfont icon-bohui"></span>
        </view>-->
                <view class="todes" @tap="toRecom">
                    <span class="iconfont icon-shoucang2"></span>
                </view>
                <view class="todes">
                    <span class="iconfont icon-erweima"></span>
                </view>
            </view>
            <view class="searchs" v-if="hidden ? true : false" v-for="(item, index) in searchs" :key="index">
                <view @tap="searchFun" :data-title="item.title">
                    {{ item.title }}
                </view>
            </view>
        </view>
        <!--历史浏览-->

        <view class="center todes-404" v-if="content == 0">
            <image src="https://i.bmp.ovh/imgs/2021/12/4fc026c69dc183fe.png"></image>
        </view>
        <!--404-->
        <view class="center todes-404" v-else-if="content == 1">
            <image src="/static/image/no.png"></image>
        </view>
        <!--新闻列表-->

        <view class="news-list" v-else>
            <!--单个新闻-->
            <view class="news-item" @tap="tourl" :data-index="index" v-for="(item, index) in newsData" :key="index">
                <!-- {{ item.allList.lenght }} -->
				
				<view style="width: 200rpx;">
					{{item.source}}
				</view>
                <view class="news-item-info">
                    <view class="header">
                        <text class="channelName" :data-index="index">{{ item.channelName }}</text>
                        <text class="source" :data-index="index">{{ item.source }}</text>
                    </view>
                    <view class="title" :data-index="index">{{ item.title }}</view>
                    <view class="time" :data-index="index">{{ item.pubDate }}</view>
                </view>

                <view class="news-item-image" v-if="item.imageurls[0] != null">
                    <image
                        :src="item.imageurls[0].url"
                        v-if="item.imageurls[0] != null"
                        style="width: 100%; height: 100%"
                        data-url="/pages/details/details"
                        :data-index="index"
                        mode="aspectFill"
                    ></image>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
// pages/todos/todos.js
const AV = require('../../utils/leancloud.js');

var app = getApp();
export default {
    data() {
        return {
            hidden: true,
            content: 0,
            search: '',
            todos: [],
            searchs: [],
            indicatorDots: true,
            autoplay: true,
            interval: 5000,
            duration: 1000,
            newsData: [],
            page: 1
        };
    },
    onReady: function () {},
    onLoad: function (options) {
        console.log('k');
        var that = this; // 页面初始化 options为页面跳转所带来的参数

        uni.getStorage({
            key: 'searchs',
            success: function (res) {
                that.setData({
                    searchs: res.data
                });
            }
        });
    },
    onReady: function () {
        // 页面渲染完成
        console.log(2);
    },
    onShow: function () {
        console.log(3);
    },
    onHide: function () {
        console.log(4); // 页面隐藏
    },
    onUnload: function () {
        console.log(5); // 页面关闭
    },
    //上拉加载
    onReachBottom: function () {
        uni.showToast({
            title: '正在加载',
            icon: 'loading',
            duration: 1000
        });
        var that = this;
        that.page = that.page + 1;
        uni.request({
            url: 'https://route.showapi.com/109-35',
            data: {
                channelId: '',
                channelName: '',
                maxResult: '15',
                needAllList: '',
                needContent: '1',
                needHtml: '',
                page: that.page,
                showapi_appid: '30851',
                title: that.search,
                showapi_sign: 'f729add89f4c4851b8da64f6936ff6f6',
                showapi_timestamp: ''
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var list = res.data.showapi_res_body.pagebean.contentlist;
                console.log(that.search, list);

                if (list.length != 0) {
                    list.forEach(function (obj) {
                        that.newsData.push(obj);
                    });
                    that.setData({
                        newsData: that.newsData,
                        page: that.page
                    });
                } else {
                    uni.showToast({
                        title: '没有了',
                        icon: 'loading',
                        duration: 1000
                    });
                }
            },
            fail: function () {
                request.error();
            }
        });
    },
    methods: {
        hiddenFun: function () {
            this.hidden = !this.hidden;
            this.setData({
                hidden: this.hidden
            });
        },

        kong: function (e) {
            this.search = '';
            this.setData({
                search: this.search
            });
        },

        //搜索页页
        searchFun: function (e) {
            var title = e.detail.value || e.target.dataset.title;
            var that = this;

            if (e.target.dataset.title) {
                that.setData({
                    search: title
                });
            }

            uni.request({
                url: 'https://route.showapi.com/109-35',
                data: {
                    channelId: '',
                    channelName: '',
                    maxResult: '15',
                    needAllList: '',
                    needContent: '1',
                    needHtml: '',
                    page: '1',
                    showapi_appid: '860473',
                    title: title,
                    showapi_sign: '772248c40b324c43b245b027004b528a',
                    showapi_timestamp: ''
                },
                header: {
                    'content-type': 'application/json'
                },
                success: function (res) {
					console.log(res);
                    if (res.data.showapi_res_body.pagebean.allNum != 0) {
                        that.newsData = res.data.showapi_res_body.pagebean.contentlist;
                        that.setData({
                            content: 2
                        });
						
                        console.log('数据', res.data.showapi_res_body.pagebean); // 添加历史记录

                        if (that.searchs.length > 8) {
                            that.searchs.pop();
                        }

                        that.searchs.unshift({
                            title: e.detail.value
                        });
                        that.setData({
                            newsData: that.newsData,
                            searchs: that.searchs
                        }); // 缓存

                        uni.setStorage({
                            key: 'searchs',
                            data: that.searchs
                        });
                    } else {
                        that.setData({
                            content: 1
                        });
                    }
                },
                fail: function () {
                    request.error();
                }
            });
        },

        tourl: function (e) {
            // object转换get字符串
            var index = e.target.dataset.index;
            var temp = this.newsData[index];
            app.globalData.array = temp;
            uni.navigateTo({
                url: '/pages/details/details'
            });
        },

        toRecom: function () {
            uni.switchTab({
                url: '/pages/recommendation/recommendation'
            });
        }
    }
};
</script>
<style>
@import './todos.css';
</style>
